const ui = require('wy-UI');
const dataManager = require('wy-DataManager');
const pomelo=require('wy-Pomelo');
cc.Class({
    extends: cc.Component,
    properties: {},
    //显示时自动注册js脚本
    onEnable: function(){
        this.node.js = this;
    },
    //自动跳转场景，预加载所有必须资源
    loadScene:function(sceneName){
        dataManager.setData('sceneName',sceneName);
        cc.director.loadScene('MjLoadding');
    },
    //预加载一个预制，不进行实例化
    loadPrefab:function(url,cb){
        cc.loader.loadRes(url, function(err, prefab){
            if(!err){
                cb(prefab);
            }else{
                cc.error(err);
            }
        });
    },
    //记载一个预制，返回实例
    loadPrefabInstantiate:function(url,cb){
        this.loadPrefab(url,(prefab)=>{
            let p=cc.instantiate(prefab);
            cb(p);
        });
    },
    //新增一个预制件，直接显示到画布
    addPrefabToCanvas:function(url,parent=cc.find('Canvas'),pos={x:0,y:0}){
        cc.loader.loadRes(url, function(err, prefab){
            if(!err){
                cc.log('加载并实例化显示组件：'+url);
                let p=cc.instantiate(prefab);
                p.x=pos.x;
                p.y=pos.y;
                parent.addChild(p);
            }
        });
    },
    loadImage:function(url,bindImage){
        if(!url || url.length < 2) return;
        if(!bindImage || !(bindImage instanceof cc.Sprite))return;
        cc.loader.load({ url: url, type: 'jpg' }, (err, texture) => {
            if(!err){
                bindImage.spriteFrame = new cc.SpriteFrame(texture); 
            }else{
                cc.error(err);
            }
        });
    },
    loadLocalImage:function(localUrl,bindImage){
        if(!bindImage || !(bindImage instanceof cc.Sprite))return;
        cc.loader.loadRes(localUrl,cc.spriteFrame, function(err, spriteFrame){
            if(!err){
                bindImage.spriteFrame=new cc.SpriteFrame(spriteFrame);
            }
        });
    },
    //"yyyy-MM-dd hh:mm:ss.S" ==> 2006-07-02 08:09:04.423 
    //"yyyy-M-d h:m:s.S"      ==> 2006-7-2 8:9:4.18 
    formatDate :function (date,fmt) { //author: meizz 
        var o = {
            "M+": date.getMonth() + 1, //月份 
            "d+": date.getDate(), //日 
            "h+": date.getHours(), //小时 
            "m+": date.getMinutes(), //分 
            "s+": date.getSeconds(), //秒 
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
            "S": date.getMilliseconds() //毫秒 
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
});
